package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.pf.PerformanceScoreMethod;
import com.plian.system.entity.sys.Office;
import com.plian.system.mapper.pf.PerformanceScoreMethodMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pf.IPerformanceScoreMethodService;
import com.plian.system.service.sys.office.IOfficeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2023/1/18 2:04 PM
 * @Version: 1.0
 **/
@Slf4j
@Service
@AllArgsConstructor
public class PerformanceScoreMethodServiceImpl extends BaseServiceImpl<PerformanceScoreMethodMapper,PerformanceScoreMethod> implements IPerformanceScoreMethodService {

    private final IOfficeService officeService;

    @Override
    public List<PerformanceScoreMethod> getByParentIds(List<Long> parentIds){
        List<PerformanceScoreMethod> scoreMethods = list(new LambdaQueryWrapper<PerformanceScoreMethod>().in(PerformanceScoreMethod::getBasePerformanceId, parentIds)
                .orderByAsc(PerformanceScoreMethod::getIndexNum));
        if (CollectionUtil.isNotEmpty(scoreMethods)){
            //处室名称
            List<Long> officeIds = scoreMethods.stream().map(PerformanceScoreMethod::getReportOffice).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList());
            List<Office> offices = officeService.listByIds(officeIds);
            scoreMethods.forEach( scoreMethod -> {
                List<Office> officeList = offices.stream().filter(m -> Optional.ofNullable(scoreMethod.getReportOffice()).isPresent() && scoreMethod.getReportOffice().equals(m.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(officeList)){
                    scoreMethod.setReportOfficeName(officeList.get(0).getName());
                }
            });
        }
        return scoreMethods;
    }

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<PerformanceScoreMethod> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PerformanceScoreMethod::getBasePerformanceId, ids);
        List<PerformanceScoreMethod> scoreMethods = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(scoreMethods)){
            return deleteLogic(scoreMethods.stream().map(PerformanceScoreMethod::getId).collect(Collectors.toList()));
        }
        return true;
    }

}
